let app = getApp();  
Page({
   /**
    * 页面的初始数据
    */
   data: {
      avatarUrl:"",
      id:"",
      userimg:"",
      username:"",
      usersex:"",
      userage:"",
      userlength:"",
      userweight:"",
      userComment:"",
   },
   upShopLogo:function(){
      var that=this;
      wx.showActionSheet({
        itemList: ['从相册中选择','拍照'],
        itemColor:"#43a2fb",
        success:function(res){
           if(!res.cancel){
              if(res.tapIndex==0){
                 that.chooseWxImageShop('album');
              }else if(res.tapIndex==1){
               that.chooseWxImageShop('camera');
              }
           }
        }
      })
   },
   chooseWxImageShop: function (type) {
      var that = this;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: [type],
        success: function (res) {
          const tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            filePath: tempFilePaths[0],
            name: 'file',
            url: 'http://1.116.140.126:8081/show_avatar',
            header: {
              'content-type': 'multipart/form-data',
              'X-AUTH-TOKEN': wx.getStorageSync('token')
            }, // 设置请求的 header
            formData: {
              // 'content-type': 'multipart/form-data',
              // 'user_id': '201825010512',
              // 'filePath': tempFilePaths[0],
            },
            success(res) {
              // const data = res.data
              var data = JSON.parse(res.data)
              console.log(data)
              //do something
              that.setData({
                avatarUrl:data
              });
              wx.showToast({
                title: '上传成功',
              })
            },
            fail: function (res) {
              wx.showToast({
                icon: 'none',
                title: '上传失败',
              })
              console.log(tempFilePaths[0])
            }
          })
        }
      })
    },
    username: function(e) {
      this.setData({
        username: e.detail.value,
      })
      console.log("姓名" + this.data.username);
    },
    radioChange:function(e){
      this.setData({
         usersex: e.detail.value,
       })
       console.log("性别" + this.data.usersex);
    },
    userage: function(e) {
      this.setData({
        userage: e.detail.value,
      })
      console.log("年龄" + this.data.userage);
    },
    userlength: function(e) {
      this.setData({
        userlength: e.detail.value,
      })
      console.log("身高" + this.data.userlength);
    },
    userweight: function(e) {
      this.setData({
        userweight: e.detail.value,
      })
      console.log("体重" + this.data.userweight);
    },
    userComment: function(e) {
      this.setData({
        userComment: e.detail.value,
      })
      console.log("备注" + this.data.userComment);
    },
    baocun: function(e) {
      console.log("姓名:" + this.data.username + "年龄:" + this.data.userage + "性别:" + this.data.usersex + "身高:" + this.data.userlength + "体重:" + this.data.userweight + "备注:" + this.data.userComment);
      wx.reLaunch({
        url: "../home/home?name="+this.data.username,
      })
    },
    formSub(e) {
      console.log('form发生了submit事件，携带数据为：', e.detail.value)
      console.log(this.data.id)
      wx.request({
        url: 'http://1.116.140.126:8081/addroles',
        method:"POST",
        data:{
          name:this.data.username,
          age:this.data.userage,
          gender:this.data.usersex,
          catagory:this.data.id,
          height:this.data.userlength,
          weight:this.data.userweight,
          comment:this.data.userComment,
          url:this.data.avatarUrl
        },
        header:{
          'conten-type':'application/json',
          'cookie':wx.getStorageSync('sessionid')
        },
        success:function(res){
          console.log(res)
        },
        fail:function(err){
        }
      })
    },
    formReset(e) {
      console.log('form发生了reset事件，携带数据为：', e.detail.value)
      this.setData({
        chosen: ''
      })
    },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      console.log(options.id)
      this.data.id = options.id
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   }
})